package org.demo.mysql.schema.controller;

import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.style.column.AbstractColumnWidthStyleStrategy;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 
 * 可以根据这里的最大宽度，按自己需要进行调整,搭配单元格样式实现类中的，自动换行，效果更好
 * @Title: ExcelCellWidthStyleStrategy
 * @Description: [自适应列宽]
 * @Author lei.cheng
 * @Date 2022/11/3
 */
public class ExcelCellWidthStyleStrategy extends AbstractColumnWidthStyleStrategy {

    private static final int MAX_COLUMN_WIDTH = 50;
    private  Map<String, Map<Integer, Integer>> CACHE = new ConcurrentHashMap<>(8);

    @Override
    protected void setColumnWidth(WriteSheetHolder writeSheetHolder, List<WriteCellData<?>> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        boolean needSetWidth = isHead || !CollectionUtils.isEmpty(cellDataList);
        String key = Objects.isNull(writeSheetHolder.getSheetNo()) ? writeSheetHolder.getSheetName(): writeSheetHolder.getSheetNo() + "";
        if (needSetWidth) {
            // 获取 key -> sheet 页各列的 宽度信息
            Map<Integer, Integer> maxColumnWidthMap = CACHE.get(key);
            if (maxColumnWidthMap == null) {
                maxColumnWidthMap = new ConcurrentHashMap<>(16);
                CACHE.put(key, maxColumnWidthMap);
            }

            // 计算当前 单元格 内容的大小
            Integer columnWidth = this.dataLength(cellDataList, cell, isHead);
            if (columnWidth >= 0) {
                if (columnWidth > MAX_COLUMN_WIDTH) {
                    columnWidth = MAX_COLUMN_WIDTH;
                }

                Integer maxColumnWidth = maxColumnWidthMap.get(cell.getColumnIndex());
                // 若当前单元格的大小大约该列最大值 更新该列宽度 并记录
                if (maxColumnWidth == null || columnWidth > maxColumnWidth) {
                    maxColumnWidthMap.put(cell.getColumnIndex(), columnWidth);
                    writeSheetHolder.getSheet().setColumnWidth(cell.getColumnIndex(), columnWidth * 256);
                }
            }
        }
    }

    private Integer dataLength(List<WriteCellData<?>> cellDataList, Cell cell, Boolean isHead) {
        if (isHead) {
            return Math.min(cell.getStringCellValue().getBytes().length, "默认宽度".getBytes().length);
            // 以内容长度作为主要宽度 head 长度不作为主要宽度
            // return cell.getStringCellValue().getBytes().length;
        } else {
            CellData<?> cellData = cellDataList.get(0);
            CellDataTypeEnum type = cellData.getType();
            if (type == null) {
                return -1;
            } else {
                switch(type) {
                    case STRING:
                        return cellData.getStringValue().getBytes().length;
                    case BOOLEAN:
                        return cellData.getBooleanValue().toString().getBytes().length;
                    case NUMBER:
                        return cellData.getNumberValue().toString().getBytes().length;
                    default:
                        return -1;
                }
            }
        }
    }
}